package com.learn.mybatis.demo1;

import com.learn.mybatis.demo1.entity.User;
import com.learn.mybatis.demo1.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        // 初始阶段
        String resource = "mybatis-config.xml";

        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        // 获取SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 第二阶段 数据读取
        try(SqlSession session = sqlSessionFactory.openSession()){
            // 找到对应接口实现
            UserMapper userMapper = session.getMapper(UserMapper.class);
            // 组装参数
            User userParam = new User();
            userParam.setSchoolName("张三学院");
            // 调用接口获取数据
            List<User> userList = userMapper.queryUserBySchoolName(userParam);
            // 打印查询到的信息
            for (User user : userList) {
                System.out.println("name:" + user.getName() + "; email:" + user.getEmail());
            }
        }
    }
}
